using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay.Data
{
    public class UD_SubmitItem : UIBaseData
    {
        #region Field

        private List<SubmitItemData> _curSelectingItems;
        private LevelSubmitData _levelSubmit;

        #endregion
        #region Properties

        public LevelSubmitData levelSubmit => _levelSubmit;

        public IReadOnlyList<SubmitItemData> curSelectingItems => _curSelectingItems;

        #endregion

        #region Interface

        public static UD_SubmitItem Create(int submitItemCid)
        {
            var d = ClassPool<UD_SubmitItem>.Get();
            d.Init(submitItemCid);
            return d;
        }

        public void Init(int submitItemCid)
        {
            _levelSubmit = new LevelSubmitData();
            levelSubmit.UpdateSubmitData(submitItemCid);
            _curSelectingItems = new List<SubmitItemData>();
            for (var i = 0; i < levelSubmit.slotCnt; i++)
            {
                _curSelectingItems.Add(null);
            }
        }


        /// <summary>
        /// 点击右侧面板Item时，将该Item对应的SubmitItemData添加到_curSelectingItems
        /// </summary>
        /// <param name="index">加入位置</param>
        /// <param name="submitItemData">加入的数据</param>
        public void AddItem(int index, SubmitItemData submitItemData)
        {
            _curSelectingItems[index] = submitItemData;
        }

        /// <summary>
        /// 点击中间面板Item时，将_curSelectingItems中该Item对应的Index的元素设置为0
        /// </summary>
        /// <param name="index"></param>
        public void RemoveItem(int index)
        {
            _curSelectingItems[index] = null;
        }

        public int FindIndex(Predicate<SubmitItemData> predicate)
        {
            return _curSelectingItems.FindIndex(predicate);
        }

        public void ClearItems()
        {
            for (var i = 0; i < _curSelectingItems.Count; i++)
            {
                _curSelectingItems[i] = null;
            }
        }

        public override void OnReset()
        {
            _curSelectingItems = null;
            _levelSubmit = null;
        }

        #endregion
    }
}